/********************************************************************************
 * BannerAccountsServiceDelegator.java
 *
 * Created by Jigar Mistry
 * Last modified Date: Sep 10, 2008 4:09:14 PM by Jigar Mistry
 * Version : 0.1
 * Copyright (c) 2008 - 2009 cyberThink Infotech Pvt. Ltd., All rights reserved.
 ********************************************************************************/

package com.printeverywhere.delegator;

import static com.printeverywhere.util.AttributeConstants.CLOSING_SESSION;

import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;

import com.printeverywhere.dto.BannerCampaigns;
import com.printeverywhere.dto.BannerDailyStatistics;
import com.printeverywhere.dto.BannerFees;
import com.printeverywhere.dto.BannerHourlyStatistics;
import com.printeverywhere.dto.BannerSettings;
import com.printeverywhere.dto.DurationDiscount;
import com.printeverywhere.util.HibernateUtil;
import com.printeverywhere.util.SearchCriteria;

/**
 * The Class BannerCampaignServiceDelegator.
 */
public class BannerCampaignServiceDelegator {

	/** The log. */
	private static Logger log = Logger.getLogger(BannerCampaignServiceDelegator.class);

	/**
	 * Gets the auction listing fees.
	 *
	 * @return the auction listing fees
	 *
	 * @throws HibernateException the hibernate exception
	 */
	@SuppressWarnings("unchecked")
	public static List<BannerCampaigns> getBannerCampaigns() throws HibernateException {
		if(log.isInfoEnabled()){
			log.info("Getting list for BannerCampaigns without search criteria".intern());
		}
		Session session = HibernateUtil.getSession();
		try {
			Transaction transaction = session.beginTransaction();
			List<BannerCampaigns> bannerCampaignsList = session.createCriteria(BannerCampaigns.class).list();
			transaction.commit();
			return bannerCampaignsList;
		} catch (HibernateException e) {
			log.error("Got exception when getting list for BannerCampaigns without search criteria".intern());
			throw e;
		} finally {
			if(log.isInfoEnabled()){
				log.info("Closing session".intern());
			}
			session.flush();
			session.close();
		}
	}

	/**
	 * Update mailing system.
	 *
	 * @param BannerCampaigns the mailing system
	 *
	 * @throws HibernateException the hibernate exception
	 */
	public static void updateBannerCampaigns(BannerCampaigns BannerCampaigns) throws HibernateException {
		if(log.isInfoEnabled()){
			log.info("Called updateBannerCampaigns method".intern());
		}
		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.update(BannerCampaigns);
			tx.commit();
		} catch (RuntimeException e) {
			log.error("Got exception when update BannerCampaigns without search criteria".intern());
			if (tx != null)
				tx.rollback();
			throw e;
		} finally {
			if(log.isInfoEnabled()){
				log.info("Closing session".intern());
			}
			session.flush();
			session.close();
		}
	}

	/**
	 * Inser banner accounts.
	 *
	 * @param BannerCampaigns the banner accounts
	 *
	 * @throws HibernateException the hibernate exception
	 */
	public static void inserBannerCampaigns(BannerCampaigns BannerCampaigns) throws HibernateException {
		if(log.isInfoEnabled()){
			log.info("Called inserBannerCampaigns method".intern());
		}
		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.save(BannerCampaigns);
			tx.commit();
		} catch (RuntimeException e) {
			log.error("Got exception when insert BannerCampaigns without search criteria".intern());
			if (tx != null)
				tx.rollback();
			throw e;
		} finally {
			if(log.isInfoEnabled()){
				log.info("Closing session".intern());
			}
			session.flush();
			session.close();
		}
	}

	/**
	 * Count banner campaigns.
	 *
	 * @param campaignId the campaign id
	 *
	 * @return the int
	 *
	 * @throws HibernateException the hibernate exception
	 */
	public static int countBannerCampaigns(int campaignId) throws HibernateException {
		if(log.isInfoEnabled()){
			log.info("Getting list for BannerCampaigns without search criteria".intern());
		}
		Session session = HibernateUtil.getSession();
		try {
			Transaction transaction = session.beginTransaction();
			Criteria criteria = session.createCriteria(BannerCampaigns.class);
			criteria.add(Restrictions.eq("user.id", campaignId));
			criteria.setProjection(Projections.rowCount());
			transaction.commit();
			return (Integer)criteria.uniqueResult();
		} catch (HibernateException e) {
			log.error("Got exception when getting list for BannerCampaigns without search criteria".intern());
			throw e;
		} finally {
			if(log.isInfoEnabled()){
				log.info("Closing session".intern());
			}
			session.flush();
			session.close();
		}
	}

	public static BannerSettings getBannerSettings() throws HibernateException {
		if(log.isInfoEnabled()){
			log.info("Getting list for BannerSettings ".intern());
		}
		Session session = HibernateUtil.getSession();
		try {
			Transaction transaction = session.beginTransaction();
			List<BannerSettings> bannerSettings = session.createCriteria(BannerSettings.class).list();
			return bannerSettings.get(0);
		} catch (HibernateException e) {
			log.error("Got exception when getting list for Banner Settings".intern());
			throw e;
		} finally {
			if(log.isInfoEnabled()){
				log.info("Closing session".intern());
			}
			session.flush();
			session.close();
		}
	}

	public static void editBannerSetting(BannerSettings settings) throws HibernateException {
		if (log.isInfoEnabled()) {
			log.info("Called editBannerSetting method".intern());
		}
		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.update(settings);
			tx.commit();
		} catch (RuntimeException e) {
			log.error("Got exception when Edit BannerSetting".intern());
			if (tx != null)
				tx.rollback();
			throw e;
		} finally {
			if (log.isInfoEnabled()) {
				log.info(CLOSING_SESSION);
			}
			session.flush();
			session.close();
		}
	}

	public static List<BannerDailyStatistics> getDailyStatisticsList(SearchCriteria searchCriteria) throws HibernateException {
		if (log.isInfoEnabled()) {
			log.info("Getting list for Campaign with search criteria".intern());
		}

		Session session = HibernateUtil.getSession();
		Criteria criteria = session.createCriteria(BannerDailyStatistics.class);
		Set<String> keys = searchCriteria.getAttributeNames();
		Iterator<String> it = keys.iterator();
		String key = null;
		Object value = null;
		while (it.hasNext()) {
			key = it.next();
			value = searchCriteria.getAttribute(key);
			criteria.add(Restrictions.eq(key, value));
		}
		Transaction transaction = session.beginTransaction();
		List<BannerDailyStatistics> list;

		try {
			list = criteria.list();
			transaction.commit();
		} catch (HibernateException e) {
			log.error("Got exception when getting list for BannerDailyStatistics without search criteria".intern());
			throw e;
		} finally {
			if (log.isInfoEnabled()) {
				log.info(CLOSING_SESSION);
			}
			session.flush();
			session.close();
		}

		return list;
	}

	public static List<BannerHourlyStatistics> getHourlyStatisticsList(SearchCriteria searchCriteria) throws HibernateException {
		if (log.isInfoEnabled()) {
			log.info("Getting list for BannerHourlyStatistics with search criteria".intern());
		}

		Session session = HibernateUtil.getSession();
		Criteria criteria = session.createCriteria(BannerHourlyStatistics.class);
		Set<String> keys = searchCriteria.getAttributeNames();
		Iterator<String> it = keys.iterator();
		String key = null;
		Object value = null;
		while (it.hasNext()) {
			key = it.next();
			value = searchCriteria.getAttribute(key);
			criteria.add(Restrictions.eq(key, value));
		}
		Transaction transaction = session.beginTransaction();
		List<BannerHourlyStatistics> list;

		try {
			list = criteria.list();
			transaction.commit();
		} catch (HibernateException e) {
			log.error("Got exception when getting list for BannerHourlyStatistics without search criteria".intern());
			throw e;
		} finally {
			if (log.isInfoEnabled()) {
				log.info(CLOSING_SESSION);
			}
			session.flush();
			session.close();
		}

		return list;
	}

	public static void updateBannerFees(BannerFees bannerFees) throws HibernateException {
		if(log.isInfoEnabled()){
			log.info("Called updateBannerFees method".intern());
		}
		Session session = HibernateUtil.getSession();
		Transaction tx = null;
		try {
			tx = session.beginTransaction();
			session.update(bannerFees);
			tx.commit();
		} catch (RuntimeException e) {
			log.error("Got exception when update updateBannerFees without search criteria".intern());
			if (tx != null)
				tx.rollback();
			throw e;
		} finally {
			if(log.isInfoEnabled()){
				log.info("Closing session".intern());
			}
			session.flush();
			session.close();
		}
	}

	/**
	 * The main method.
	 *
	 * @param args the arguments
	 */
	public static void main(String[] args) {
		getBannerCampaigns();

	}
}